// pages/savemoney/savemoney.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    LTS_addr: "https://lts.cn-east-3.myhuaweicloud.com",
    basic: 0,
    allfornow: 0,
    allhour: 0,
    hour: 0,
    overtime: 0,
    leavetime: 0,
    inputovertime:0,
    inputleavetime:0,
  },

  updateAllfornow(){
    var ret = this.data.basic + this.data.hour*(this.data.overtime - this.data.leavetime);
    this.data.allfornow = ret
    console.log(ret)
    this.setData({
      allfornow: ret,
    })
  },

  updateAllData(all){
    this.data.basic = all["basic"]
    this.data.hour = all["hour"]
    this.data.overtime = all["overtime"]
    this.data.leavetime = all["leavetime"]
    this.data.allfornow = all["allfornow"]
  },
  clearAllData(){
    console.log("数据已清零")
    this.data.basic = 0
    this.data.hour = 0
    this.data.overtime = 0
    this.data.leavetime = 0
    this.data.allfornow = 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("1")
  },

  onConfirm(event) {
    console.log(this.data.hour, this.data.overtime, this.data.leavetime, this.data.basic)
    var value = parseFloat(event.detail.value);
    console.log('确认输入:', value);
    if(isNaN(value)){
      console.log('input is NaN')
      value = 0;
    }
    console.log("id:" + event.currentTarget.id)
    if (event.currentTarget.id == 1) {
      this.data.hour = value
    } else if (event.currentTarget.id == 2) {
      this.data.inputovertime = value
    } else if (event.currentTarget.id == 3) {
      this.data.inputleavetime = value
    } else if (event.currentTarget.id == 4) {
      this.data.basic = value
      this.setData({
        basic: value
      })
    }
    console.log(this.data.hour, this.data.overtime, this.data.leavetime, this.data.basic)
  },
  ConfirmToCloud: async function () {
    console.log(this.data.hour, this.data.overtime, this.data.leavetime, this.data.basic)
    this.data.overtime += this.data.inputovertime
    this.data.leavetime += this.data.inputleavetime
    this.updateAllfornow()
    await app.writeTodatabase("work", {
      "hour": this.data.hour,
      "overtime": this.data.overtime,
      "leavetime": this.data.leavetime,
      "basic": this.data.basic,
      "allfornow":this.data.allfornow,
    })
  },
  CleanCloudData: async function(){
    this.data.overtime = 0
    this.data.leavetime = 0
    this.updateAllfornow()
    await app.writeTodatabase("work", {
      "hour": this.data.hour,
      "overtime": 0,
      "leavetime": 0,
      "basic": this.data.basic,
      "allfornow":this.data.allfornow,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function() {
    console.log("show:", this.data.basic)
    this.data.inputleavetime = 0
    this.data.inputovertime = 0
    var alldata = await app.readDatabase("work")
    console.log(typeof alldata)
    if(typeof alldata == 'object' &&"hour" in alldata && "overtime" in alldata && "leavetime" in alldata &&  "basic" in alldata && "allfornow" in alldata){
      this.setData({
        basic: alldata["basic"],
        allfornow: alldata["allfornow"]
      })
      this.updateAllData(alldata);
    }else{
      this.clearAllData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("4")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log("5")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log("6")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log("7")
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log("7")
  }
})